import { core, GameObject, getGameObjectById } from "./gameengine";
import { State, StateMachine } from "./StateMachine";
import { user } from "./user";
import { MonsterBehaviour, FightUI } from "./monster";
import { fightUIPrefab } from "./data";

class Marshal extends StateMachine {

}

export class MonsterMarshalMachine extends StateMachine {


    changeState = (state) => {

        if (this.currentState) {
            const currentStateType = this.currentState['__proto__'].constructor.name;
            const stateType = state['__proto__'].constructor.name;

            if (currentStateType != stateType) {
                this.currentState.onExit();
                this.currentState = state;
                this.currentState.onEnter()
            }
        }
        else {
            this.currentState = state;
            this.currentState.onEnter();

        }
    }

}

export class MarshalStartState extends State {

    onEnter() {
        core.loadScene('assets/gaming/start.scene.json')
    }

    onExit() {
    }
}

export class MarshalFinishState extends State {

    onEnter() {
        core.loadScene('assets/gaming/finishi.scene.json')
    }

    onExit() {
    }
}

export class MarshalShuomingState extends State {

    onEnter() {
        core.loadScene('assets/gaming/shuoming.scene.json')
    }

    onExit() {
    }
}

export class MarshalFirstState extends State {

    onEnter() {
        core.loadScene('assets/gaming/first.scene.json')
    }

    onExit() {
    }
}

export class MarshalSecondState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Second.scene.json')
    }

    onExit() {
    }
}

export class MarshalThirdState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Third.scene.json')
    }

    onExit() {
    }
}
export class MarshalFourthState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Fourth.scene.json')
    }

    onExit() {
    }
}

export class MarshalFifthState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Fifth.scene.json')
    }

    onExit() {
    }
}

export class MarshalSixthState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Sixth.scene.json')
    }

    onExit() {
    }
}

export class MarshalSeventhState extends State {

    onEnter() {
        core.loadScene('assets/gaming/Seventh.scene.json')
    }

    onExit() {
    }
}

export class MarshalOpeningState extends State {

    onEnter() {
        core.loadScene('assets/opening.scene.json')
    }

    onExit() {
    }
}

export class MarshalLandingState extends State {

    onEnter() {
        core.loadScene('assets/landing.scene.json')
    }

    onExit() {
    }
}

export class MarshalGamingState extends State {

    onEnter() {
        user.score = 0;
        user.hp = 100;
        core.loadScene("assets/gaming.scene.json")
    }

    onExit() {
    }
}

export class MarshalEndingState extends State {

    onEnter() {
        core.loadScene("assets/ending.json")
    }

    onExit() {

    }
}
export class MarshalTestState extends State {

    onEnter() {
        core.loadScene("assets/rect.scene.json")
    }

    onExit() {

    }
}

export class MarshalMonsterIdleState extends State {

    onExit() {
        console.log("怪物闲置状态退出")
    }

    onEnter() {
        console.log("怪物闲置状态开始")
    }
}

export class MarshalMonsterAttackState extends State {

    monster: GameObject = null;

    constructor(monster) {
        super()
        this.monster = monster;
    }

    onExit() {
        console.log("怪物攻击状态退出")
    }

    onEnter() {
        const monsterBehaviour = this.monster.getScript(MonsterBehaviour)
        const fightUI = core.createPrefab(fightUIPrefab);
        console.log(fightUI)
        const fight = fightUI.getScript(FightUI);
        fight.fight(this.monster)
        const scene = getGameObjectById("Scene");
        scene.addChild(fightUI)
        monsterBehaviour.isChange = true;
        monsterBehaviour.gameObject = this.monster;
        monsterBehaviour.fightUI = fightUI
        // 100
        // for (; monsterBehaviour.hp > 0; monsterBehaviour.hp--) {
        //     console.log(monsterBehaviour.hp)
        // }
        // 0

        console.log("怪物攻击状态开始")


    }
}



export const marshal = new Marshal();